﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Web.Core;
using Web.Core.Objects;

public partial class AdminCP_AdminControls_AddEditArticle : System.Web.UI.UserControl
{
    public int artID = 0;
    public string header = "";
    string cDo = "";

    protected void Page_Load(object sender, EventArgs e)
    {

        cDo = Request.Params["do"];
        if (cDo != null)
        {
            InitControl();
        }
    }

    void InitControl()
    {
        if ((int)Session["Language"] == GlobalStorage.Language.Vietnamese)
            this.scriptEpoch.InnerHtml = "<script type=\"text/javascript\" src=\"../Js/epoch_classes_vi.js\"></script>";
        else if ((int)Session["Language"] == GlobalStorage.Language.English)
            this.scriptEpoch.InnerHtml = "<script type=\"text/javascript\" src=\"../Js/epoch_classes.js\"></script>";
        if (Request.Params["aid"] != null)
        {
            artID = int.Parse(Request.Params["aid"]);
        }

        this.drptype.Items.Add("about");
        this.drptype.Items.Add("project");
        this.drptype.Items.Add("client");
        this.drptype.Items.Add("news");
        this.drptype.Items.Add("gallery");
        this.drptype.Items.Add("pattern");

        this.drpActive.Items.Add(Resources.Lang.Yes);
        this.drpActive.Items.Add(Resources.Lang.No);
        this.drptop.Items.Add(Resources.Lang.Yes);
        this.drptop.Items.Add(Resources.Lang.No);

        switch (cDo)
        {
            case "edit":
                header = Resources.Lang.Edit;
                Article artvi = Articles.getAllArticleByID(artID, GlobalStorage.Language.Vietnamese);
                Article arten = Articles.getAllArticleByID(artID, GlobalStorage.Language.English);
                this.txtNameEn.Text = arten.Title;
                this.txtNameVi.Text = artvi.Title;
                this.txaDesCatEn.Value = arten.Content;
                this.txaDesCatVi.Value = artvi.Content;
                this.txtImage.Text = arten.Image;
                this.txtSequence.Text = arten.Sequence.ToString();

                this.drptype.Text = arten.Type;
                if (arten.Active == true)
                    this.drpActive.SelectedIndex = 0;
                else
                    this.drpActive.SelectedIndex = 1;

                if (arten.Top == true)
                    this.drptop.SelectedIndex = 0;
                else
                    this.drptop.SelectedIndex = 1;
                break;
            case "add":
                header = Resources.Lang.Add;
                break;
            default:
                break;
        }

        // Validator        
        this.RequiredFieldValidatorNameEn.ErrorMessage = Resources.Lang.RequireNameEn + ". ";
        this.RequiredFieldValidatorNameVi.ErrorMessage = Resources.Lang.RequiteNameVi + ".";
       // this.RequiredFieldValidatortxaDesCatEn.ErrorMessage = Resources.Lang.Require;
        //this.RequiredFieldValidatortxaDesCatVi.ErrorMessage = Resources.Lang.Require;
    }

    protected void btnOK_Click(object sender, EventArgs e)
    {
        string img = ""; int sequence = 0;
        try
        {
            img = this.txtImage.Text;
        }
        catch { }
        try
        {
            sequence = int.Parse(this.txtSequence.Text);
        }
        catch { }

        string type = this.drptype.Text;
        bool active ,top;

        if (this.drpActive.Text == Resources.Lang.Yes)
            active = true;
        else
            active = false;
        if(this.drptop.Text == Resources.Lang.Yes)
            top = true;
        else
            top = false;

        if (cDo == "edit")
        {
            Article art = new Article();
            art.Id = artID;
            art.Type = type;
            art.Sequence = sequence;
            art.Image = img;
            art.Top = top;
            art.Active = active;
            ShopContent content = new ShopContent(this.txtNameEn.Text, this.txtNameVi.Text, this.txaDesCatEn.Value, this.txaDesCatVi.Value);
            Articles.UpdateArticle(art, content);
        }
        else if (cDo == "add")
        {
            Article art = new Article();
            art.Type = type;
            art.Sequence = sequence;
            art.Image = img;
            art.Top = top;
            art.Active = active;
            ShopContent content = new ShopContent(this.txtNameEn.Text, this.txtNameVi.Text, this.txaDesCatEn.Value, this.txaDesCatVi.Value);
            Articles.InsertArticle(art, content);
        }
        Response.Redirect("Articles.aspx");
    }
}
